package com.zeldu.myclass.controller;

import com.zeldu.common.model.response.CommonCode;
import com.zeldu.common.model.response.QueryResponseResult;
import com.zeldu.common.model.response.QueryResult;
import com.zeldu.common.model.response.ResponseResult;
import com.zeldu.myclass.pojo.Department;
import com.zeldu.myclass.pojo.MyClass;
import com.zeldu.myclass.pojo.Student;
import com.zeldu.myclass.service.DepartmentService;
import com.zeldu.myclass.service.MyClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class MyClassController {
    @Autowired(required = false)
    private MyClassService myClassService;
    @Autowired(required = false)
    private DepartmentService departmentService;

    @GetMapping("/getClasses")
    public QueryResponseResult<MyClass> getClasses(Long id) {
        QueryResult<MyClass> queryResult = new QueryResult<>();
        List<MyClass> classList = myClassService.getClasses(id);
        queryResult.setList(classList);
        queryResult.setTotal(classList.size());

        return new QueryResponseResult<MyClass>(CommonCode.SUCCESS, queryResult);
    }

    @PostMapping("/addClass")
    public ResponseResult addClass(@RequestBody MyClass myClass) {
        if (myClassService.addClass(myClass) != 0) {
            return new ResponseResult((CommonCode.SUCCESS));
        } else {
            return new ResponseResult((CommonCode.FAIL));
        }

    }

    @GetMapping("/addDepartment")
    public ResponseResult addDepartment(Department department) {
        if (departmentService.addDepartment(department) != 0) {
            System.out.println("Success");
            return new ResponseResult((CommonCode.SUCCESS));
        } else {
            return new ResponseResult((CommonCode.FAIL));
        }

    }

    @GetMapping("/delClass")
    public ResponseResult delClass(Long id) {
        if (myClassService.deleteClass(id) != 0) {
            return new ResponseResult((CommonCode.SUCCESS));
        } else {
            return new ResponseResult((CommonCode.FAIL));
        }
    }

    @GetMapping("/delDepartment")
    public ResponseResult delDepartment(Long id) {
        if (departmentService.deleteDepartment(id) != 0) {
            return new ResponseResult((CommonCode.SUCCESS));
        } else {
            return new ResponseResult((CommonCode.FAIL));
        }
    }

    @GetMapping("/removeStudent")
    public ResponseResult removeStudent(Long id) {
        if (myClassService.removeStudent(id) != 0) {
            return new ResponseResult((CommonCode.SUCCESS));
        } else {
            return new ResponseResult((CommonCode.FAIL));
        }
    }

    @GetMapping("/getStudents")
    public QueryResponseResult<Student> getStudents(Long id) {
        QueryResult<Student> queryResult = new QueryResult<>();
        List<Student> studentList = myClassService.getStudents(id);
        queryResult.setList(studentList);
        queryResult.setTotal(studentList.size());
        return new QueryResponseResult<Student>(CommonCode.SUCCESS, queryResult);
    }

    @GetMapping("getDepartments")
    public QueryResponseResult<Department> getDepartments() {
        QueryResult<Department> queryResult = new QueryResult<>();
        List<Department> departmentList = departmentService.getDepartments();
        queryResult.setList(departmentList);
        queryResult.setTotal(departmentList.size());

        return new QueryResponseResult<Department>(CommonCode.SUCCESS, queryResult);
    }
}
